package com.sy.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sy.PageVO;
import com.sy.dto.CoursesByUidDTO;
import com.sy.dto.CoursesPageByUserDTO;
import com.sy.dto.CoursesPageDTO;
import com.sy.mapper.ChaptersMapper;
import com.sy.mapper.CoursesMapper;
import com.sy.mapper.SectionsMapper;
import com.sy.pojo.Chapters;
import com.sy.pojo.Courses;
import com.sy.pojo.Sections;
import com.sy.service.CoursesService;
import com.sy.vo.HotCourseVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @className: CoursesServiceImpl
 * @author: Mr.Liu
 * @date: 2025/10/29 13:57
 * @Version: 1.0
 * @description:课程业务实现
 */
@Service
public class CoursesServiceImpl extends ServiceImpl<CoursesMapper, Courses> implements CoursesService {
    @Resource
    private CoursesMapper coursesMapper;
    @Resource
    private ChaptersMapper chaptersMapper;
    @Resource
    private SectionsMapper sectionsMapper;

    @Override
    public PageVO<Courses> queryPage(CoursesPageDTO coursesPageDTO) {
        PageHelper.startPage(coursesPageDTO.getCurPage(), coursesPageDTO.getPageSize());
        List<Courses> list = coursesMapper.queryCourses(coursesPageDTO.getName(), coursesPageDTO.getStatus()
                , coursesPageDTO.getTypeId());
        //遍历所有课程
        for (Courses courses : list) {
            //课程id
            Integer coursesId = courses.getId();
            //通过课程id查询章节信息-->List<Chapters>
            List<Chapters> chaptersList = chaptersMapper.selectList(
                    new QueryWrapper<Chapters>()
                            .eq("courseId", coursesId)
            );
            //将根据课程id查询出来的章节集合赋值给course中的chaptersList属性
            courses.setChaptersList(chaptersList);
            //继续遍历chaptersList-->给 Chapters中sections属性赋值
            for (Chapters chapters : chaptersList) {
                //获取章节id-->根据章节id查询小节信息-->List<Sections>
                Integer chaptersId = chapters.getId();
                List<Sections> sectionsList = sectionsMapper.selectList(
                        new QueryWrapper<Sections>()
                                .eq("chapterId", chaptersId)
                );
                //给属性赋值
                chapters.setSections(sectionsList);
            }
        }

        PageInfo<Courses> pageInfo = new PageInfo<>(list);
        //建造者模式
        return PageVO.<Courses>builder()
                .curPage(pageInfo.getPageNum())
                .pageSize(pageInfo.getPageSize())
                .total(pageInfo.getTotal())
                .dataList(pageInfo.getList())
                .build();

    }

    @Override
    public PageVO<Courses> queryCoursesPage(CoursesPageByUserDTO coursesPageDTO) {
        PageHelper.startPage(coursesPageDTO.getCurPage(), coursesPageDTO.getPageSize());
        List<Courses> list = coursesMapper.queryCoursesByPage(coursesPageDTO.getName()
                , coursesPageDTO.getTypeId());
        //遍历所有课程
        for (Courses courses : list) {
            //课程id
            Integer coursesId = courses.getId();
            //通过课程id查询章节信息-->List<Chapters>
            List<Chapters> chaptersList = chaptersMapper.selectList(
                    new QueryWrapper<Chapters>()
                            .eq("courseId", coursesId)
            );
            //将根据课程id查询出来的章节集合赋值给course中的chaptersList属性
            courses.setChaptersList(chaptersList);
            //继续遍历chaptersList-->给 Chapters中sections属性赋值
            for (Chapters chapters : chaptersList) {
                //获取章节id-->根据章节id查询小节信息-->List<Sections>
                Integer chaptersId = chapters.getId();
                List<Sections> sectionsList = sectionsMapper.selectList(
                        new QueryWrapper<Sections>()
                                .eq("chapterId", chaptersId)
                );
                //给属性赋值
                chapters.setSections(sectionsList);
            }
        }


        PageInfo<Courses> pageInfo = new PageInfo<>(list);
        //建造者模式
        return PageVO.<Courses>builder()
                .curPage(pageInfo.getPageNum())
                .pageSize(pageInfo.getPageSize())
                .total(pageInfo.getTotal())
                .dataList(pageInfo.getList())
                .build();

    }

    @Override
    @Transactional  //事务，要么全部添加成功，要么全部失败
    public void insertCourses(Courses courses) {
        //获取课程id
        Integer courseId = courses.getId();
        //获取当前课程中所有章节
        List<Chapters> chaptersList = courses.getChaptersList();
        for (Chapters chapter : chaptersList) {
            //给章节中的课程id赋值,上传时间
            chapter.setCourseid(courseId);
            chapter.setCreateTime(LocalDateTime.now());
            //开始添加章节信息
            //如果添加成功我们在映射文件中添加了主键回填,可以直接获取到添加章节的id
            chaptersMapper.insert(chapter);
            int chapterId = chapter.getId();
            //章节中还有小节呢，集合
            //获取章节中小节集合
            List<Sections> sectionsList = chapter.getSections();
            //遍历过程中添加
            for (Sections sections : sectionsList) {
                //给缺失的属性赋值
                sections.setChapterid(chapterId);
                sections.setCreateTime(LocalDateTime.now());
                sectionsMapper.insert(sections);
            }
        }
    }

    @Override
    public Courses queryById(Integer id) {
        Courses courses = coursesMapper.selectByPrimaryKey(id);
        //courses中chapterList属性是null，因此需要赋值
        //根据课程id查询章节信息
        List<Chapters> chaptersList = chaptersMapper.selectList(new QueryWrapper<Chapters>()
                .eq("courseId", courses.getId())
        );
        //给集合中的Chapters中sections属性赋值
        for (Chapters chapters : chaptersList) {
            //根据章节id查询小节信息
            List<Sections> sections = sectionsMapper.selectList(
                    new QueryWrapper<Sections>()
                            .eq("chapterId", chapters.getId())
            );
            chapters.setSections(sections);
        }
        courses.setChaptersList(chaptersList);
        return courses;
    }

    @Override
    public List<Courses> queryCoursesByUid(CoursesByUidDTO coursesByUidDTO) {

        return coursesMapper.findCoursesByUid(
                coursesByUidDTO.getUid(),
                coursesByUidDTO.getCourseName(),
                coursesByUidDTO.getCourseType(),
                coursesByUidDTO.getSortBy());
    }

    @Override
    public List<HotCourseVO> hotCourse(Integer limit) {
        return coursesMapper.courseHot(limit);
    }
}
